import Vue from 'vue';
var mpheader = require("../components/mpheader.js");
var repaymentList = require("./repaymentList.js");
var dateTime = require("../components/dateTime.js");
var num = require("../components/number.js");

var theRepayVue, commonData;
var credits = [];
// 实例还款界面
function createRepayVue(eachPay, commonData1) {
    commonData = commonData1;
    var mheder1 = mpheader.getMPheader({
        "titleText": "扣款计划表"
    }, goBack2);
//    credits = computeRepayListData();
    var repayData = computeRepayData(eachPay, commonData);
    repayData.credits = credits;
    var repayCells = repaymentList.getRepayCells(repayData);
    bindSubscribeLoanAmoun();
    theRepayVue = new Vue({
        el: "#app_repay",
        data: repayData,
        computed: {

        },
        components: {
            "mpheader": mheder1,
            "repaycells": repayCells
        },
        methods: {
            goBack: function() {
                commonData.step = 2;
            }
        }
    });

    return theRepayVue;
}

// 计算创建每期的还款数据
function computeRepayData(eachPay, commonData) {
    var repayData = {
        rateArray: eachPay,
        amount: "",
        brokerage: "",
        commonData: commonData,
        credits: ""
    };

    return repayData;
}


// 计算每期还款额和时间
function computeRepayListData(acdata,loanAmoun) {
    var newcredits = [];
    var eachPrincipal = num.hold2bit(loanAmoun / parseInt(acdata.stage));
    for(var i=0,l=acdata.stage;i<l;i++){
        var repayDate = new Date(nowTormatDate);
        var dateNum = repayDate.getDate() + 25*i;
        repayDate.setDate(dateNum);
        var thereypayDate = dateTime.getDateStrFormate2("yyyy-mm-dd",repayDate);
        var criditsObject ={
            stageNum: i+1,
            repayDate:thereypayDate,
            total:acdata.repayment,
            principal:eachPrincipal,
            brokerage:(parseFloat(acdata.repayment) - parseFloat(eachPrincipal)).toFixed(2)
            
        };

        newcredits.push(criditsObject);
    }
    theRepayVue.credits = newcredits;
    return credits;
   
}

// 设置还款计划前的进度条
function setPaymentCell(acdata) {
    var paymentChild = '';
    for(var i=0; i<acdata.stage; i++){
        var isFrist = '';
        var isfail = '';
        if(i ==0){
            isFrist = 'frist-circle';
        }
        
        if(i != 0){
            paymentChild += '<div class="line-box"><div class="line"></div></div>';
            
        }
        if(i == acdata.stage-1){
            isFrist = 'last-circle'
        }
        paymentChild += '<div class="circle-box"><div class="circle ' + isFrist + ' ' + isfail +'"></div></div>';
    }
    setTimeout(function  () {
      $("#payment-speed").html(paymentChild);  
    },400);
}

function goBack2() {
    commonData.step = 2;
}

// 监听输入金额改变
function bindSubscribeLoanAmoun () {
    $.subscribe("loanAmounChange", function(event,acdata,loanAmoun) {
        console.dir(acdata);
        theRepayVue.amount = acdata.contentMonet;
        theRepayVue.brokerage = acdata.saleRate;
        computeRepayListData(acdata,loanAmoun);
        setPaymentCell(acdata);
        console.log("loanAmounChange");
    });
}


module.exports = {
    createRepayVue: createRepayVue
}